package org.jground.zk.lab;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.jground.dbfs.DBFile;
import org.jground.rx.ui.zk.docu.UIDocuUtils;
import org.jground.util.IOUtils;
import org.jground.zk.zulX.WindowX;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Fileupload;

public class UpAndDownloadWindow extends WindowX {
	public UpAndDownloadWindow() {
		Button b = new Button("download");
		add(b);

		b.addEventListener(Events.ON_CLICK, new EventListener() {

			public void onEvent(Event event) throws Exception {
				FileInputStream in = new FileInputStream(
						"e:/tab/person/toni/album2009/20090110/2009_01_10__20_35-dsc00938-2009_01_10__20_35.jpg");
				Filedownload.save(in, null, "x.jpg");

			}
		});
		b = new Button("upload");
		add(b);
		b.addEventListener(Events.ON_CLICK, new EventListener() {

			public void onEvent(Event event) throws Exception {
				Media media = Fileupload.get();
				String s = media.getName();
				byte[] bytes = null;
				if (media.isBinary()) {
					bytes = media.getByteData();
				} else {
					bytes = media.getStringData().getBytes(DBFile.encoding);
				}

				InputStream in = media.getStreamData();
				IOUtils.copy(in, new FileOutputStream(s));
				IOUtils.closeQuietly(in);
			}
		});
	}

}
